#include "httpserverhandler.h"
#include <QJsonDocument>
#include <QJsonObject>

using namespace stefanfrings;

HttpServerHandler::HttpServerHandler(QObject* parent)
    : HttpRequestHandler(parent), businessLogic(new BusinessLogic()) {}

void HttpServerHandler::service(HttpRequest& request, HttpResponse& response) {
    // 设置响应格式为JSON
    response.setHeader("Content-Type", "application/json; charset=utf-8");

    // 根据请求路径分发处理
    QByteArray path = request.getPath();
    if (path == "/api/attend/list" && request.getMethod() == "GET") {
        handleGetAttendList(request, response);
    }
    else if (path == "/api/attend/add" && request.getMethod() == "POST") {
        handleAddAttendRecord(request, response);
    }
    else {
        // 404 Not Found
        response.setStatus(404, "Not Found");
        response.write("{\"error\":\"Not found\"}", true);
    }
}

void HttpServerHandler::handleGetAttendList(HttpRequest& request, HttpResponse& response) {
    // 调用业务逻辑层获取数据
    QJsonArray result = businessLogic->getAttendList();
    response.write(QJsonDocument(result).toJson(), true);
}


void HttpServerHandler::handleAddAttendRecord(HttpRequest& request, HttpResponse& response){

}
